package com.tledu.web_01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * 通过注解进行配置
 */
@WebServlet(name = "student", urlPatterns = "/stu", loadOnStartup = 1,initParams = {
        @WebInitParam(name = "kkk",value = "对应的值")
})
public class StudentServlet extends HttpServlet {
    private String msg;

    @Override
    public void init() throws ServletException {
        super.init();
        this.msg = "初始化值";
        System.out.println(this.getServletConfig().getInitParameter("kkk"));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String msg = req.getParameter("msg");
        System.out.println(msg);
        System.out.println(req.getMethod());
        System.out.println(req.getContentType());
        System.out.println(req.getQueryString());
        System.out.println(req.getContextPath());
        System.out.println(req.getRequestURI());
        System.out.println(req.getRemoteHost());
        System.out.println(req.getRemotePort());
        System.out.println(req.getServletPath());
        // 可能会吧鉴权信息放到请求里
        System.out.println(req.getHeader(""));
        System.out.println("===============================");

        // 把获取到的数据写到界面上
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println("<h1>这是一个学生的请求</h1>");
        resp.getWriter().println("<div>留言内容：" + msg + "</div>");
        resp.getWriter().println("<div>这是执行init初始化的值" + this.msg + "</div>");
        resp.getWriter().println("<div>老师是" + req.getAttribute("teacher") + "</div>");
        // 获取到这个数据进行展示
        List<String> list = (List<String>) this.getServletContext().getAttribute("list");
        for (String name : list) {
            resp.getWriter().println("<div>名字：" + name + "</div>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
//        String name = req.getParameter("name");
        // 通过IO流，字符流来读取数据
        BufferedReader bf = req.getReader();
        StringBuilder stringBuilder = new StringBuilder();
        while (true) {
            String line = bf.readLine();
            if (line == null) {
                break;
            }
            stringBuilder.append(line);
        }
        System.out.println(stringBuilder.toString());
        // 通过java格式化json数据，通过fastjson处理json数据
        // 使用这些类库的时候
        JSONObject jsonObject = JSON.parseObject(stringBuilder.toString());
        // jsonObject就是键值对，使用起来和HashMap类似

        System.out.println(req.getMethod());
        // 设置相应体的类型
        resp.setContentType("text/html;charset=utf-8");
        // 设置相应体
        resp.getWriter().println("这个一个post请求");
        resp.getWriter().println(jsonObject.get("name"));
    }

    @Override
    public void destroy() {
        super.destroy();
        System.out.println("servlet被销毁了");
    }
}
